﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietThuongLMS.ViewModels;
using VietThuongLMS.Services;
using DCS.ComLib.Config;
using VietThuongLMS.Utils;
using VietThuongLMS.Areas.Admin.Models;
using System.IO;

///Author: NHDuc
///Created Date: 25/12/2013
namespace VietThuongLMS.Areas.Admin.Controllers
{
    public class HinhAnhController : Controller
    {
        //
        // GET: /Admin/HinhAnh/

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Index(int id)
        {
            if (!Request.IsAuthenticated)
                return RedirectToAction("Login", "Home", new { area = "Admin" });

            int page = 1;
            if (Request["p"] != null)
            {
                page = int.Parse(Request["p"].ToString());
            }

            HinhAnhViewModel HinhAnhList = HinhAnhService.GetHinhAnhAllList(id, int.Parse(ConfigReader.ReadString("ItemPerPage")), page);

            //phân trang
            Pagination pagination = new Pagination(true, Url.Content("~"), "Admin", "HinhAnh", "Index", "?id=" + id + "&p=", HinhAnhList.Total, page, int.Parse(ConfigReader.ReadString("ItemPerPage")), "Previous", "Next", "First", "End");
            string pageLinks = pagination.GetPageLinks();
            ViewData["pageLinks"] = pageLinks;
            //end phân trang

            ViewData["id"] = id;
            return View(HinhAnhList);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Detail(int id)
        {
            HinhAnh HinhAnh = HinhAnhService.GetHinhAnhById(id);
            return View(HinhAnh);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Create(int id)
        {
            ViewData["id"] = id;
            Album album = AlbumService.GetAlbumById(id);
            ViewData["TenAlbum"] = album.TenAlbum;
            ViewData["type"] = album.Type;
            return View();
        }

        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(HinhAnhModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    HinhAnh data = new HinhAnh();
                    data.TenAnh = model.TenAnh;
                    //data.Tag = model.Tag;
                    //data.Type = model.Type;
                    data.AlbumID = model.AlbumId;
                    data.NgayLap = DateTime.Now;

                    //https://www.youtube.com/watch?v=XNe3CC8d7Ys
                    if (model.UrlVideo != null && !model.UrlVideo.Equals(""))
                    {
                        data.UrlVideo = model.UrlVideo;
                        if (model.UrlVideo.IndexOf("v=") != -1)
                            data.UrlVideo = model.UrlVideo.Substring(model.UrlVideo.IndexOf("v=") + 2,
                                model.UrlVideo.Length - model.UrlVideo.IndexOf("v=") - 2);
                    }
                    
                    //url
                    if (model.Url != null && !model.Url.FileName.Equals(""))
                    {
                        if (model.Url.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("Url", "(*) Hình ảnh không được phép quá 2MB");
                            ViewData["id"] = model.ID;
                            Album album = AlbumService.GetAlbumById(model.ID);
                            ViewData["TenAlbum"] = album.TenAlbum;
                            ViewData["type"] = album.Type;
                            return View("Create");
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.Url.FileName);
                            DCS.ComLib.File.Log.Write("HinhAnhController::Create", "", this.Server.MapPath("~/Images/HoatDong/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.Url, 320, this.Server.MapPath("~/Images/HoatDong/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("HinhAnhController::Create", ex.StackTrace, ex.Message);
                            }
                            data.Url = "Images/HoatDong/" + fileName;
                        }
                    }
                    ///

                    bool result = HinhAnhService.InsertHinhAnh(data);
                    if (result)
                        return RedirectToAction("Index", "HinhAnh", new { area = "Admin", id = model.AlbumId });
                    return RedirectToAction("Index", "HinhAnh", new { area = "Admin", id = model.AlbumId });
                }
                catch (Exception ex)
                {

                }
            }
            return View(model);
        }

        //delete Account
        [Authorize(Roles = "Admin,NhanVien")]
        [HttpGet]
        public ActionResult Delete(int id, int albumid)
        {
            bool result = HinhAnhService.DeleteHinhAnh(id);
            if (result)
            {
                return RedirectToAction("Index", "HinhAnh", new { area = "Admin", id = albumid });
            }
            else
            {
                return RedirectToAction("Index", "HinhAnh", new { area = "Admin", id = albumid });
            }

        }

        [Authorize(Roles = "Admin,NhanVien")]
        public ActionResult Edit(int id)
        {
            HinhAnh HinhAnh = HinhAnhService.GetHinhAnhById(id);
            HinhAnhModel model = (new HinhAnhConverter()).HinhAnhToModel(HinhAnh);
            Album album = AlbumService.GetAlbumById(HinhAnh.AlbumID.Value);
            ViewData["id"] = album.ID;
            ViewData["TenAlbum"] = album.TenAlbum;
            ViewData["type"] = album.Type;
            return View(model);
        }

        [Authorize(Roles = "Admin,NhanVien")]
        [HttpPost, ValidateInput(false)]
        public ActionResult Edit(HinhAnhModel model)
        {
            //if (ModelState.IsValid)
            //{
                try
                {
                    HinhAnh data = new HinhAnh();
                    data.ID = model.ID;
                    data.TenAnh = model.TenAnh;
                    //data.Tag = model.Tag;
                    //data.Type = model.Type;
                    //url
                    if (model.Url != null && !model.Url.FileName.Equals(""))
                    {
                        if (model.Url.ContentLength > 2097152)
                        {
                            ModelState.AddModelError("Url", "(*) Hình ảnh không được phép quá 2MB");
                            HinhAnh HinhAnh = HinhAnhService.GetHinhAnhById(model.ID);
                            HinhAnhModel model1 = (new HinhAnhConverter()).HinhAnhToModel(HinhAnh);
                            Album album2 = AlbumService.GetAlbumById(HinhAnh.AlbumID.Value);
                            ViewData["id"] = album2.ID;
                            ViewData["TenAlbum"] = album2.TenAlbum;
                            ViewData["type"] = album2.Type;
                            return View("Edit", model1);
                        }
                        else
                        {
                            string fileName = Path.GetFileName(model.Url.FileName);
                            DCS.ComLib.File.Log.Write("HinhAnhController::Edit", "", this.Server.MapPath("~/Images/HoatDong/"));
                            try
                            {
                                fileName = Utils.Upload.UpdateImageWithSize(model.Url, 320, this.Server.MapPath("~/Images/HoatDong/"));
                            }
                            catch (Exception ex)
                            {
                                DCS.ComLib.File.Log.Write("HinhAnhController::Edit", ex.StackTrace, ex.Message);
                            }
                            data.Url = "Images/HoatDong/" + fileName;
                        }
                    }
                    if (model.UrlVideo != null)
                        data.UrlVideo = model.UrlVideo;

                    bool result = HinhAnhService.Edit(data);
                    Album album = AlbumService.GetAlbumById(model.AlbumId);
                    ViewData["TenAlbum"] = album.TenAlbum;
                    ViewData["type"] = album.Type;
                    if (result)
                        return RedirectToAction("Index", "HinhAnh", new { area = "Admin", id = model.AlbumId });
                    return RedirectToAction("Index", "HinhAnh", new { area = "Admin", id = model.AlbumId });
                }
                catch (Exception ex)
                {
                    return RedirectToAction("Index", "Album", new { area = "Admin"});
                }
            //}
            //return View(model);
        }

    }
}
